package com.example.autoclick2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_OVERLAY_PERMISSION = 100;
    private FloatWindow floatWindow;
    private Button btnAccessibility, btnOverlay, btnShowFloat, btnSetMode, btnSetInterval;
    private RadioGroup rgClickMode;
    private EditText etInterval;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        btnAccessibility = findViewById(R.id.btn_accessibility);
        btnOverlay = findViewById(R.id.btn_overlay);
        btnShowFloat = findViewById(R.id.btn_show_float);
        btnSetMode = findViewById(R.id.btn_set_mode);
        btnSetInterval = findViewById(R.id.btn_set_interval);
        rgClickMode = findViewById(R.id.rg_click_mode);
        etInterval = findViewById(R.id.et_interval);

        // 设置无障碍服务按钮点击事件
        btnAccessibility.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到无障碍服务设置页面
                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(intent);
            }
        });

        // 设置悬浮窗权限按钮点击事件
        btnOverlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestOverlayPermission();
            }
        });

        // 设置显示悬浮窗按钮点击事件
        btnShowFloat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkPermissions()) {
                    showFloatWindow();
                }
            }
        });

        // 设置点击模式按钮点击事件
        btnSetMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setClickMode();
            }
        });

        // 设置间隔按钮点击事件
        btnSetInterval.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setInterval();
            }
        });

        // 初始化悬浮窗
        floatWindow = new FloatWindow(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateButtonStatus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (floatWindow != null) {
            floatWindow.hide();
        }
    }

    // 请求悬浮窗权限
    private void requestOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
        } else {
            Toast.makeText(this, "已获得悬浮窗权限", Toast.LENGTH_SHORT).show();
        }
    }

    // 检查所需权限
    private boolean checkPermissions() {
        // 检查悬浮窗权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "请先授予悬浮窗权限", Toast.LENGTH_SHORT).show();
            return false;
        }

        // 检查无障碍服务是否开启
        if (!AutoClickService.isServiceRunning()) {
            Toast.makeText(this, "请先开启无障碍服务", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    // 显示悬浮窗
    private void showFloatWindow() {
        if (floatWindow != null) {
            floatWindow.show();
            Toast.makeText(this, "悬浮窗已显示", Toast.LENGTH_SHORT).show();
        }
    }

    // 设置点击模式
    private void setClickMode() {
        int selectedId = rgClickMode.getCheckedRadioButtonId();
        int mode = 0;
        
        if (selectedId == R.id.rb_single_click) {
            mode = 0;
        } else if (selectedId == R.id.rb_double_click) {
            mode = 1;
        } else if (selectedId == R.id.rb_swipe_up) {
            mode = 2;
        } else if (selectedId == R.id.rb_swipe_down) {
            mode = 3;
        }
        
        // 设置到无障碍服务
        if (AutoClickService.isServiceRunning()) {
            AutoClickService service = AutoClickService.getInstance();
            if (service != null) {
                service.setClickMode(mode);
                // 更新悬浮窗显示
                if (floatWindow != null) {
                    floatWindow.updateModeDisplay();
                }
            }
        } else {
            Toast.makeText(this, "请先开启无障碍服务", Toast.LENGTH_SHORT).show();
        }
    }

    // 设置点击间隔
    private void setInterval() {
        String intervalText = etInterval.getText().toString();
        if (intervalText.isEmpty()) {
            Toast.makeText(this, "请输入间隔时间", Toast.LENGTH_SHORT).show();
            return;
        }
        
        try {
            int interval = Integer.parseInt(intervalText);
            
            // 设置到无障碍服务
            if (AutoClickService.isServiceRunning()) {
                AutoClickService service = AutoClickService.getInstance();
                if (service != null) {
                    service.setInterval(interval);
                }
            } else {
                Toast.makeText(this, "请先开启无障碍服务", Toast.LENGTH_SHORT).show();
            }
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }

    // 更新按钮状态
    private void updateButtonStatus() {
        // 更新悬浮窗权限按钮状态
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(this)) {
            btnOverlay.setText("悬浮窗权限(已获取)");
            btnOverlay.setEnabled(false);
        } else {
            btnOverlay.setText("获取悬浮窗权限");
            btnOverlay.setEnabled(true);
        }

        // 更新无障碍服务按钮状态
        if (AutoClickService.isServiceRunning()) {
            btnAccessibility.setText("无障碍服务(已开启)");
        } else {
            btnAccessibility.setText("开启无障碍服务");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OVERLAY_PERMISSION) {
            updateButtonStatus();
        }
    }
}